$(function () {
    //下面是代码的抽取操作
    var departmentDatagrid,editAndQuitBtn,departmentDialog,editForm,rolebox;
    departmentDatagrid = $('#department_datagrid');
    editAndQuitBtn = $('#editBtn,#quitBtn');
    departmentDialog = $('#department_dialog');
    editForm = $('#editForm');
    rolebox=$("#department_roles");
    departmentDatagrid.datagrid({
        url:'/department/list.do',
        fit:true,
        fitColumns:true,
        pagination:true,
        pageNumber:1,
        pageSize:10,
        toolbar:"#tb",
        singleSelect:true,
        rownumbers:true,
        columns:[[
            {field:'id',title:'id',width:100,align:"center"},
            {field:'sn',title:'部门编号',width:100,align:"center"},
            {field:'name',title:'部门名称',width:100,align:"center"},
            {field:'state',title:'状态',width:100,align:"center",formatter:stateFormatter},
        ]],
        onSelect:function (index,row) {
            if (row.state == -1) {
                //在这里是把组建都禁止
                editAndQuitBtn.linkbutton("disable");
            } else {
                editAndQuitBtn.linkbutton("enable");
            }
        }
    });
    var comdObj = {
        add:function () {
            departmentDialog.dialog("open");
            departmentDialog.dialog("setTitle","部门新增");
            editForm.form("clear");
        },
        cancel:function () {
            departmentDialog.dialog("close");
        },
        save:function () {
            //1.异步的提交表单
            editForm.form("submit",{
                url:"/department/saveOrUpdate.do",
                success:function (data) {
                    data = $.parseJSON(data);
                    if (data.success){
                        $.messager.alert("温馨提示:",data.msg,"info",function () {
                            departmentDatagrid.datagrid("reload");
                            departmentDialog.dialog("close");
                        })
                    }else {
                        $.messager.alert("温馨提示:",data.msg,"error");
                    }
                }
            });
        },
        edit:function () {
            var rowData = departmentDatagrid.datagrid("getSelected");
            if(!rowData){
                $.messager.alert("温馨提示:","请选中要编辑的信息","info");
            }else{
                editForm.form("clear");
                editForm.form("load",rowData);
                departmentDialog.dialog("setTitle","部门编辑");
                departmentDialog.dialog("open");
            }
        },
        remove:function () {
            var rowData = departmentDatagrid.datagrid("getSelected");
            if(!rowData){
                $.messager.alert("温馨提示:","请选中要离职的部门","info");
            }else{
                $.messager.confirm("温馨提示:","亲,确定要离职该部门",function (flag) {
                    if(flag){
                        $.get("/department/updateState.do",{id:rowData.id},function (data) {
                            if(data.success){
                                $.messager.alert("温馨提示:",data.msg,"info",function () {
                                    //刷新当前的页面
                                    departmentDatagrid.datagrid("reload");
                                });
                            }else {
                                $.messager.alert("温馨提示:",data.msg,"error");
                            }
                        });
                    }
                });
            }
        },
        reload:function () {
            departmentDatagrid.datagrid("reload");
        }
    }
    departmentDialog.dialog({
        width: 400,
        height: 300,
        closed: true,
        buttons:"#dialog_bt"
    });
    $("a[data-cmd]").click(function () {
        var op = $(this).data("cmd");
        comdObj[op]();
    });
});
//转化函数
function stateFormatter(val) {
    if(val==0){
        return "<span style='color: green'>正常</span>";
    }
    return "<span style='color: red'>离职</span>";
};

